import queue


class mission:
    def __init__(self, slide_size, zero_level_queue):
        self.slide_size = slide_size
        self.first_level_energy = slide_size[0]
        self.second_level_energy = slide_size[1]
        self.third_level_energy = slide_size[2]
        self.now_time = 0
        self.zero_level_queue = zero_level_queue
        self.first_level_queue = queue.Queue()
        self.second_level_queue = queue.Queue()
        self.third_level_queue = queue.Queue()
        self.finish_queue = queue.Queue()

    def check(self):
        if self.zero_level_queue.qsize() == 0 and self.first_level_queue.qsize() == 0 and self.second_level_queue.qsize() == 0 and self.third_level_queue.qsize() == 0:
            return 0

    def show_queue(self):
        queue_list = list(self.zero_level_queue.queue)
        print("等待队列:")
        for item in queue_list:
            # 访问元素的属性并输出内容
            print(item.name, item.receive_time, item.need_time, item.rest_time)
        queue_list = list(self.first_level_queue.queue)
        print("一级队列:")
        for item in queue_list:
            # 访问元素的属性并输出内容
            print(item.name, item.receive_time, item.need_time, item.rest_time)
        queue_list = list(self.second_level_queue.queue)
        print("二级队列:")
        for item in queue_list:
            # 访问元素的属性并输出内容
            print(item.name, item.receive_time, item.need_time, item.rest_time)
        queue_list = list(self.third_level_queue.queue)
        print("三级队列:")
        for item in queue_list:
            # 访问元素的属性并输出内容
            print(item.name, item.receive_time, item.need_time, item.rest_time)
        queue_list = list(self.finish_queue.queue)
        print("结束队列:")
        for item in queue_list:
            # 访问元素的属性并输出内容
            print(item.name, item.receive_time, item.need_time, item.rest_time, item.end_time)

    def run(self):
        self.now_time += 1
        for i in range(self.zero_level_queue.qsize()):
            # 检查所有等待队列的进程
            t = self.zero_level_queue.queue[0]
            if t.receive_time <= self.now_time:
                # 如果时间到了, 则把他放入第一队列
                self.first_level_queue.put(self.zero_level_queue.get())
        if self.first_level_queue.qsize() != 0:
            # 若不为空则检查第一队列

            self.first_level_queue.queue[0].rest_time -= 1  # 所需时间减一
            self.first_level_energy -= 1
            if self.first_level_queue.queue[0].rest_time == 0:
                # 第一队列头运行结束
                self.first_level_energy = self.slide_size[0]  # 重新计算时间片
                self.first_level_queue.queue[0].end_time = self.now_time  # 记录结束时间
                self.finish_queue.put(self.first_level_queue.get())  # 从队列中删除
            if self.first_level_energy == 0:
                # 第一队列头时间片用完
                self.first_level_energy == self.slide_size[0]  # 重新计算时间片
                self.second_level_queue.put(self.first_level_queue.get())  # 放入第二队列
        elif self.second_level_queue.qsize() != 0:
            # 第一队列为空且第二对列不为空

            self.second_level_queue.queue[0].rest_time -= 1  # 所需时间减一
            self.second_level_energy -= 1
            if self.second_level_queue.queue[0].rest_time == 0:
                # 第二 队列头运行结束
                self.second_level_energy = self.slide_size[0]  # 重新计算时间片
                self.second_level_queue.queue[0].end_time = self.now_time  # 记录结束时间
                self.finish_queue.put(self.second_level_queue.get())  # 从队列中删除
            if self.second_level_energy == 0:
                # 第二 队列头时间片用完
                self.second_level_energy == self.slide_size[1]  # 重新计算时间片
                self.third_level_queue.put(self.second_level_queue.get())  # 放入第二队列
        elif self.third_level_queue.qsize() != 0:
            # 仅剩第三队列不为空
            self.third_level_queue.queue[0].rest_time -= 1  # 所需时间减一
            self.third_level_energy -= 1
            if self.third_level_queue.queue[0].rest_time == 0:
                # 第三 队列头运行结束
                self.third_level_energy = self.slide_size[0]  # 重新计算时间片
                self.third_level_queue.queue[0].end_time = self.now_time  # 记录结束时间
                self.finish_queue.put(self.third_level_queue.get())  # 从队列中删除
            if self.third_level_energy == 0:
                # 第三 队列头时间片用完
                self.third_level_energy == self.slide_size[2]  # 重新计算时间片
                self.third_level_queue.put(self.third_level_queue.get())
        print("第%d秒结束时:" % self.now_time)


class Process:
    def __init__(self, name, receive_time, need_time):
        self.name = name
        self.receive_time = receive_time
        self.need_time = need_time
        self.rest_time = need_time


if __name__ == "__main__":
    P0 = Process("P0", 0, 3)
    P1 = Process("P1", 2, 2)
    P2 = Process("P2", 3, 3)
    P = queue.Queue()
    P.put(P0)
    P.put(P1)
    P.put(P2)
    m = mission([2, 4, 8], P)
    while 1:
        m.run()
        m.show_queue()
        if m.check() == 0:
            break
